package com.ciwong.epaper.modules.bookstore.ui;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.epaper.bean.NewBook;
import com.ciwong.epaper.modules.epaper.ui.AllServiceOrBookActivity;
import com.ciwong.epaper.modules.epaper.util.EpaperJumpManager;
import com.ciwong.epaper.modules.epaper.util.ItemBookOrService;
import com.ciwong.epaper.util.DisplayUtils;
import com.ciwong.epaper.widget.RecycleViewDivider;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/12/29.
 */

public class ItemGoodServiceList extends LinearLayout{


    private  RecyclerView item_recyclerview;
    private  GoodServiceHorizontalAdapter mGoodServiceHorizontalAdapter;

    private  TextView tx_no_book_tips;


    public ItemGoodServiceList(Context context) {
        super(context);
        LayoutInflater.from(this.getContext()).inflate(R.layout.item_good_service_list, this);
        item_recyclerview=findViewById(R.id.item_recyclerview);
        tx_no_book_tips=findViewById(R.id.tx_no_book_tips);

        init();
    }

    protected void init() {
        item_recyclerview.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        mGoodServiceHorizontalAdapter = new GoodServiceHorizontalAdapter();
        item_recyclerview.addItemDecoration(new RecycleViewDivider(getContext(), LinearLayoutManager.HORIZONTAL, DisplayUtils.dip2px(getContext().getApplicationContext(),1), getResources().getColor(R.color.light_gray6)));
        item_recyclerview.setAdapter(mGoodServiceHorizontalAdapter);

        findViewById(R.id.btn_service_check_all).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {

                EpaperJumpManager.jumpToAllServiceOrBook(R.string.go_back, (Activity) getContext(), AllServiceOrBookActivity.TYPE_HOT_GOOD_SERVICE, 0,null);
            }
        });
    }




    public void update(List<NewBook> newBookList){

        if(null != newBookList && newBookList.size()>0)
        {
            mGoodServiceHorizontalAdapter.setNewBookArrayList(newBookList);
            item_recyclerview.setVisibility(VISIBLE);
            tx_no_book_tips.setVisibility(GONE);
        }else {
            newBookList =new ArrayList<>();
            mGoodServiceHorizontalAdapter.setNewBookArrayList(newBookList);
            tx_no_book_tips.setVisibility(VISIBLE);
            item_recyclerview.setVisibility(GONE);
        }

    }

    /**
     * 内层的RecyclerView
     */
    private class GoodServiceHorizontalAdapter extends RecyclerView.Adapter<GoodServiceHolder> {

        private List<NewBook> newBookArrayList = new ArrayList<>();

        private GoodServiceHorizontalAdapter() {

        }

        public void setNewBookArrayList(List<NewBook> list) {
            newBookArrayList = list;
            notifyDataSetChanged();
        }

        @Override
        public GoodServiceHolder onCreateViewHolder(ViewGroup parent, int viewType) {

            return new GoodServiceHolder(new ItemBookOrService(parent.getContext()));

        }

        /**
         * @param holder
         * @param position
         */
        @Override
        public void onBindViewHolder(GoodServiceHolder holder, final int position) {
            Uri uri;
            if (null != newBookArrayList && newBookArrayList.size()>0 /*position < newBookArrayList.size()*/)
            {
                NewBook newBook = newBookArrayList.get(position);
                holder.mItemBookOrService.update(newBook);


//                holder.tv_subject.setText(subject.getSubjectName());
//                holder.img_subject.setImageResource( R.mipmap.chinese);
//                holder.mView.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
////                        MallJumpManager.jumpToGoodsList(0, (Activity) getContext(), appointmentTypeList.get(position), GoodsListActivity2.SEARCH_TYPE_GOODS_LIST_BY_CODE, GdConstants.ENTRANCE_TYPE_APPOINTMENT);
//                    }
//                });

            }

        }

        @Override
        public int getItemCount() {
            if (null != newBookArrayList) {
                return newBookArrayList.size();
            } else {
                return 0;
            }
        }
    }

    class GoodServiceHolder extends RecyclerView.ViewHolder {
        ItemBookOrService mItemBookOrService;
        public GoodServiceHolder(ItemBookOrService itemView) {
            super(itemView);
            mItemBookOrService=itemView;

        }
    }


    protected View inflate(ViewGroup parent, int layoutRes) {
        return LayoutInflater.from(parent.getContext()).inflate(layoutRes, parent, false);
    }


    private void setNetImg(SimpleDraweeView simpleDraweeView, Uri uri) {
        ImageRequest request=null;
        if(uri!=null)
        {
            request= ImageRequestBuilder.newBuilderWithSource(uri)
                    .setResizeOptions(new ResizeOptions(DisplayUtils.dip2px(getContext(),300),DisplayUtils.dip2px(getContext(),420))).build();

            DraweeController drawweeController= Fresco.newDraweeControllerBuilder().setUri(uri).setImageRequest(request)
                    //列表滑动会变流畅
                    .setOldController(simpleDraweeView.getController())
//                    .setAutoPlayAnimations(true)
                    .build();
            simpleDraweeView.setController(drawweeController);
        }

    }
}
